package com.example.Project.user;

import com.example.Project.Class.MassOrganizations;
import com.example.Project.user.userHttpMethods.userMethods;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

public class ShowMasterMassController {
    private static int id;
    void start() throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("showMasterMass.fxml"));
        Scene scene = new Scene((Parent)fxmlLoader.load(), 677.0, 402.0);
        Stage stage=new Stage();
        stage.setTitle("社团信息管理");
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }
    void get(int theId){
        id=theId;
        System.out.println(id);
    }
    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ContextMenu menu;

    /**
     * 退出社团
     * @param actionEvent
     */
    public void btnLook(ActionEvent actionEvent) throws IOException {
        LookApplyController enter=new LookApplyController();
        enter.get(table.getSelectionModel().getSelectedItem().getId());
        enter.start();
    }

    @FXML
    private TableView<MassOrganizations> table;
    @FXML
    private TableColumn<MassOrganizations, Integer> tableId;
    @FXML
    private TableColumn<MassOrganizations, String> tableName;
    @FXML
    private TableColumn<MassOrganizations, Date> tableBirthday;
    @FXML
    private TableColumn<MassOrganizations, String> tableType;
    @FXML
    private TableColumn<MassOrganizations, String> tablePhone;
    com.example.Project.user.userHttpMethods.userMethods userMethods=new userMethods();
    @FXML
    public void initialize() throws ClassNotFoundException, IOException {
        table.getItems().clear();
        ObservableList<MassOrganizations> massOrganizationsList= userMethods.getMasterMass(id);
        if(massOrganizationsList!=null) {
            System.out.println(massOrganizationsList);
            Platform.runLater(() -> updateTableView(massOrganizationsList));
        }else{
            Alert al = new Alert(Alert.AlertType.CONFIRMATION);
            al.setHeaderText("你还不是社长，暂时无可管理的社团！！！");
            al.showAndWait();
        }
    }
    private void updateTableView(List<MassOrganizations> MassOrganizations) {
        this.table.getItems().addAll(MassOrganizations);
        this.tableId.setCellValueFactory(new PropertyValueFactory<>("Id"));
        this.tableName.setCellValueFactory(new PropertyValueFactory<>("Name"));
        this.tableBirthday.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        this.tableType.setCellValueFactory(new PropertyValueFactory<>("Type"));
        this.tablePhone.setCellValueFactory(new PropertyValueFactory<>("Phone"));
    }
}
